package com.beta.password_menu;

import java.util.List;

import com.beta.nfc_beta.R;
import com.beta.nfc_beta.vars.Vars;
import com.beta.password_menu.LockPatternView.Cell;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ConfirmPasswordActivity extends Activity {

	private SharedPreferences AppIni;

	private String packageName;

	private Intent i;
	private boolean isPasswordRight;
	private Button btnCpLeft;
	private TextView TopText, BottomText;
	private LockPatternView mLockPatternView;

	private ActivityManager mActivityManager;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.confirmpassword);

		i = new Intent();
		i = ConfirmPasswordActivity.this.getIntent();
		isPasswordRight = false;
		Bundle dd = this.getIntent().getExtras();
		mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		packageName = dd.getString("PackageName");
		btnCpLeft = (Button) findViewById(R.id.btnCpLeft);
		TopText = (TextView) findViewById(R.id.CpTopText);
		BottomText = (TextView) findViewById(R.id.CpBottomText);
		mLockPatternView = (LockPatternView) findViewById(R.id.CpLockPattern);

		TopText.setText(R.string.confirmpassword);
		BottomText.setText(R.string.need_to_unlock);
		btnCpLeft.setText(R.string.retry);
		

		AppIni = getSharedPreferences("configure", Context.MODE_WORLD_WRITEABLE
				| Context.MODE_WORLD_READABLE);

		btnCpLeft.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				mLockPatternView.clearPattern();
				if (isPasswordRight) {

				}
			}
		});
		
		
		mLockPatternView
				.setOnPatternListener(new LockPatternView.OnPatternListener() {
					public void onPatternStart() {
						TopText.setText(R.string.pattern_start);
					}

					public void onPatternCleared() {
						Log.v("msg", "Cleared");
					}

					public void onPatternCellAdded(List<Cell> pattern) {
					}

					public void onPatternDetected(List<Cell> pattern) {
						String passwordTmp = AppIni.getString("Password", null);
						if (passwordTmp.equals(LockPatternUtils
								.patternToString(pattern))) {
							TopText.setText(null);
							isPasswordRight = true;
							mLockPatternView
									.setDisplayMode(LockPatternView.DisplayMode.Correct);
							ConfirmPasswordActivity.this.finish();
						} else {
							TopText.setText(R.string.input_wrong);
							mLockPatternView
									.setDisplayMode(LockPatternView.DisplayMode.Wrong);
						}
					}
				});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		long exitTime = System.currentTimeMillis();
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			if((System.currentTimeMillis()-exitTime) > 2000){ 
				Toast.makeText(getApplicationContext(), "Tap again to exit", Toast.LENGTH_SHORT).show();
			
			} else { 
		
				
				System.exit(0); 
			} 
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	protected void exitProgram() { 
	    finish(); 
	    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
	    int sdk = Integer.valueOf(Build.VERSION.SDK).intValue(); 
	    if (sdk < 8) { 
	        activityManager.restartPackage(getPackageName()); 
	    } else { 
	        activityManager.killBackgroundProcesses(getPackageName()); 
	    } 
	    System.exit(0); 
	} 
}

